#!/usr/bin/make -f

#
# \brief  Prepare Genode build directory
# \author Christian Helmuth, Norman Feske
# \date   2008-08-14
#

MAKEOVERRIDES =

PLATFORM = $(MAKECMDGOALS)

usage:
	@echo
	@echo "Tool for preparing Genode build directories"
	@echo
	@echo "usage:"
	@echo
	@echo "  create_builddir <platform> BUILD_DIR=<build-dir> GENODE_DIR=<genode-dir>"
	@echo
	@echo "  <platform>   can be 'linux_x86', 'fiasco_x86', or 'pistachio_x86'"
	@echo "  <build-dir>  is the location of the build directory to create"
	@echo "  <genode-dir> top-level directory of the Genode source tree"
	@echo
	@echo "When creating a build directory for 'fiasco_x86', you must further"
	@echo "set 'L4_DIR' to the build directory of the L4 environment."
	@echo "When creating a build directory for 'pistachio_x86', you must further"
	@echo "set 'L4_DIR' to the user-land build directory of Pistachio."
	@echo

ifeq ($(BUILD_DIR),)
$(PLATFORM):: usage
	@echo "Error: BUILD_DIR not defined." ; false
endif
ifeq ($(GENODE_DIR),)
$(PLATFORM):: usage
	@echo "Error: GENODE_DIR not defined." ; false
endif
ifeq ($(L4_DIR),)
fiasco_x86 pistachio_x86:: usage
	@echo "Error: L4_DIR not defined." ; false
endif

#
# Convert GENODE_DIR to an absolute directory because the user
# may have specified a '~'-relative location or a pwd-relative
# location.
#
GENODE_ABS_DIR := $(realpath $(shell echo $(GENODE_DIR)))

#
# Generic build-directory preparation rules
#
BUILD_CONF = $(GENODE_ABS_DIR)/tool/builddir/etc/build.conf.$(PLATFORM)

$(BUILD_DIR)/etc:
	@mkdir -p $@

$(BUILD_DIR)/etc/build.conf: $(BUILD_CONF)
	@echo "GENODE_DIR   = $(GENODE_ABS_DIR)" > $@
	@cat $< >> $@

$(BUILD_DIR)/Makefile:
	@ln -sf $(GENODE_ABS_DIR)/tool/builddir/build.mk $@

#
# Platform-specific build-directory preparation rules
#
$(BUILD_DIR)/etc/fiasco.conf:
	@echo "L4_DIR = $(L4_DIR)" > $@

$(BUILD_DIR)/etc/pistachio.conf:
	@echo "PISTACHIO_USER_BUILD_DIR = $(L4_DIR)" > $@

$(BUILD_DIR)/etc/tools.conf:
	@echo "CROSS_DEV_PREFIX = /usr/local/genode-gcc/bin/genode-x86-" > $@

#
# Generic platform dependencies
#
$(PLATFORM):: $(BUILD_DIR)/etc
$(PLATFORM):: $(BUILD_DIR)/etc/build.conf
$(PLATFORM):: $(BUILD_DIR)/Makefile

#
# Platform-specific dependencies
#
fiasco_x86:: $(BUILD_DIR)/etc/fiasco.conf $(BUILD_DIR)/etc/tools.conf

pistachio_x86:: $(BUILD_DIR)/etc/pistachio.conf $(BUILD_DIR)/etc/tools.conf

$(PLATFORM)::
	@echo "successfully created build directory at $(BUILD_DIR)"

.PHONY: $(PLATFORM)
